{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 11.13 \u53d1\u9001\u4e0e\u63a5\u6536\u5927\u578b\u6570\u7ec4\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8981\u901a\u8fc7\u7f51\u7edc\u8fde\u63a5\u53d1\u9001\u548c\u63a5\u53d7\u8fde\u7eed\u6570\u636e\u7684\u5927\u578b\u6570\u7ec4\uff0c\u5e76\u5c3d\u91cf\u51cf\u5c11\u6570\u636e\u7684\u590d\u5236\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u51fd\u6570\u5229\u7528 memoryviews \u6765\u53d1\u9001\u548c\u63a5\u53d7\u5927\u6570\u7ec4\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# zerocopy.py\n\ndef send_from(arr, dest):\n    view = memoryview(arr).cast('B')\n    while len(view):\n        nsent = dest.send(view)\n        view = view[nsent:]\n\ndef recv_into(arr, source):\n    view = memoryview(arr).cast('B')\n    while len(view):\n        nrecv = source.recv_into(view)\n        view = view[nrecv:]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u901a\u8fc7socket\u8fde\u63a5\u7684\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u7a0b\u5e8f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socket import *\ns = socket(AF_INET, SOCK_STREAM)\ns.bind(('', 25000))\ns.listen(1)\nc,a = s.accept()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5ba2\u6237\u7aef\uff08\u53e6\u5916\u4e00\u4e2a\u89e3\u91ca\u5668\u4e2d\uff09\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socket import *\nc = socket(AF_INET, SOCK_STREAM)\nc.connect(('localhost', 25000))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u7684\u76ee\u6807\u662f\u4f60\u80fd\u901a\u8fc7\u8fde\u63a5\u4f20\u8f93\u4e00\u4e2a\u8d85\u5927\u6570\u7ec4\u3002\u8fd9\u79cd\u60c5\u51b5\u7684\u8bdd\uff0c\u53ef\u4ee5\u901a\u8fc7 array \u6a21\u5757\u6216 numpy \u6a21\u5757\u6765\u521b\u5efa\u6570\u7ec4\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy\na = numpy.arange(0.0, 50000000.0)\nsend_from(a, c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy\na = numpy.zeros(shape=50000000, dtype=float)\na[0:10]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "recv_into(a, c)\na[0:10]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6570\u636e\u5bc6\u96c6\u578b\u5206\u5e03\u5f0f\u8ba1\u7b97\u548c\u5e73\u884c\u8ba1\u7b97\u7a0b\u5e8f\u4e2d\uff0c\u81ea\u5df1\u5199\u7a0b\u5e8f\u6765\u5b9e\u73b0\u53d1\u9001/\u63a5\u53d7\u5927\u91cf\u6570\u636e\u5e76\u4e0d\u5e38\u89c1\u3002\n\u4e0d\u8fc7\uff0c\u8981\u662f\u4f60\u786e\u5b9e\u60f3\u8fd9\u6837\u505a\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u4f60\u7684\u6570\u636e\u8f6c\u6362\u6210\u539f\u59cb\u5b57\u8282\uff0c\u4ee5\u4fbf\u7ed9\u4f4e\u5c42\u7684\u7f51\u7edc\u51fd\u6570\u4f7f\u7528\u3002\n\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u5c06\u6570\u636e\u5207\u5272\u6210\u591a\u4e2a\u5757\uff0c\u56e0\u4e3a\u5927\u90e8\u5206\u548c\u7f51\u7edc\u76f8\u5173\u7684\u51fd\u6570\u5e76\u4e0d\u80fd\u4e00\u6b21\u6027\u53d1\u9001\u6216\u63a5\u53d7\u8d85\u5927\u6570\u636e\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u67d0\u79cd\u673a\u5236\u5e8f\u5217\u5316\u6570\u636e\u2014\u2014\u53ef\u80fd\u5c06\u5176\u8f6c\u6362\u6210\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u3002\n\u4e0d\u8fc7\uff0c\u8fd9\u6837\u6700\u7ec8\u4f1a\u521b\u5efa\u6570\u636e\u7684\u4e00\u4e2a\u590d\u5236\u3002\n\u5c31\u7b97\u4f60\u53ea\u662f\u96f6\u788e\u7684\u505a\u8fd9\u4e9b\uff0c\u4f60\u7684\u4ee3\u7801\u6700\u7ec8\u8fd8\u662f\u4f1a\u6709\u5927\u91cf\u7684\u5c0f\u578b\u590d\u5236\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u901a\u8fc7\u4f7f\u7528\u5185\u5b58\u89c6\u56fe\u5c55\u793a\u4e86\u4e00\u4e9b\u9b54\u6cd5\u64cd\u4f5c\u3002\n\u672c\u8d28\u4e0a\uff0c\u4e00\u4e2a\u5185\u5b58\u89c6\u56fe\u5c31\u662f\u4e00\u4e2a\u5df2\u5b58\u5728\u6570\u7ec4\u7684\u8986\u76d6\u5c42\u3002\u4e0d\u4ec5\u4ec5\u662f\u90a3\u6837\uff0c\n\u5185\u5b58\u89c6\u56fe\u8fd8\u80fd\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u8f6c\u6362\u6210\u4e0d\u540c\u7c7b\u578b\u6765\u8868\u73b0\u6570\u636e\u3002\n\u8fd9\u4e2a\u5c31\u662f\u4e0b\u9762\u8fd9\u4e2a\u8bed\u53e5\u7684\u76ee\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "view = memoryview(arr).cast('B')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4 arr\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u65e0\u7b26\u53f7\u5b57\u8282\u7684\u5185\u5b58\u89c6\u56fe\u3002\u8fd9\u4e2a\u89c6\u56fe\u80fd\u88ab\u4f20\u9012\u7ed9socket\u76f8\u5173\u51fd\u6570\uff0c\n\u6bd4\u5982 socket.send() \u6216 send.recv_into() \u3002\n\u5728\u5185\u90e8\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u76f4\u63a5\u64cd\u4f5c\u8fd9\u4e2a\u5185\u5b58\u533a\u57df\u3002\u4f8b\u5982\uff0csock.send() \u76f4\u63a5\u4ece\u5185\u5b58\u4e2d\u53d1\u751f\u6570\u636e\u800c\u4e0d\u9700\u8981\u590d\u5236\u3002\nsend.recv_into() \u4f7f\u7528\u8fd9\u4e2a\u5185\u5b58\u533a\u57df\u4f5c\u4e3a\u63a5\u53d7\u64cd\u4f5c\u7684\u8f93\u5165\u7f13\u51b2\u533a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5269\u4e0b\u7684\u4e00\u4e2a\u96be\u70b9\u5c31\u662fsocket\u51fd\u6570\u53ef\u80fd\u53ea\u64cd\u4f5c\u90e8\u5206\u6570\u636e\u3002\n\u901a\u5e38\u6765\u8bb2\uff0c\u6211\u4eec\u5f97\u4f7f\u7528\u5f88\u591a\u4e0d\u540c\u7684 send() \u548c recv_into() \u6765\u4f20\u8f93\u6574\u4e2a\u6570\u7ec4\u3002\n\u4e0d\u7528\u62c5\u5fc3\uff0c\u6bcf\u6b21\u64cd\u4f5c\u540e\uff0c\u89c6\u56fe\u4f1a\u901a\u8fc7\u53d1\u9001\u6216\u63a5\u53d7\u5b57\u8282\u6570\u91cf\u88ab\u5207\u5272\u6210\u65b0\u7684\u89c6\u56fe\u3002\n\u65b0\u7684\u89c6\u56fe\u540c\u6837\u4e5f\u662f\u5185\u5b58\u8986\u76d6\u5c42\u3002\u56e0\u6b64\uff0c\u8fd8\u662f\u6ca1\u6709\u4efb\u4f55\u7684\u590d\u5236\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u6709\u4e2a\u95ee\u9898\u5c31\u662f\u63a5\u53d7\u8005\u5fc5\u987b\u4e8b\u5148\u77e5\u9053\u6709\u591a\u5c11\u6570\u636e\u8981\u88ab\u53d1\u9001\uff0c\n\u4ee5\u4fbf\u5b83\u80fd\u9884\u5206\u914d\u4e00\u4e2a\u6570\u7ec4\u6216\u8005\u786e\u4fdd\u5b83\u80fd\u5c06\u63a5\u53d7\u7684\u6570\u636e\u653e\u5165\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6570\u7ec4\u4e2d\u3002\n\u5982\u679c\u6ca1\u529e\u6cd5\u77e5\u9053\u7684\u8bdd\uff0c\u53d1\u9001\u8005\u5c31\u5f97\u5148\u5c06\u6570\u636e\u5927\u5c0f\u53d1\u9001\u8fc7\u6765\uff0c\u7136\u540e\u518d\u53d1\u9001\u5b9e\u9645\u7684\u6570\u7ec4\u6570\u636e\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}